package cn.stylefeng.guns.modular.business.frequency.banner.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.enums.CoreExceptionEnum;
import cn.stylefeng.guns.core.exception.CustomException;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.base.entity.BaseEntity;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.custom.constant.PublicCommon;
import cn.stylefeng.guns.modular.business.frequency.banner.entity.Banner;
import cn.stylefeng.guns.modular.business.frequency.banner.enums.BannerExceptionEnum;
import cn.stylefeng.guns.modular.business.frequency.banner.mapper.BannerMapper;
import cn.stylefeng.guns.modular.business.frequency.banner.model.param.BannerParam;
import cn.stylefeng.guns.modular.business.frequency.banner.service.BannerService;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 轮播图 服务实现类
 *
 * @author 刘耀
 * @date 2022/02/01 11:05
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {


    @Autowired
    private BusinessUtil businessUtil;

    @Override
    public PageResult<Banner> page(BannerParam bannerParam) {

        // 构造条件
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Banner::getDelFlag, PublicCommon.删除);
        if(!LoginContextHolder.me().isSuperAdmin()){
            List<Long> tenantIdList = new ArrayList<>();
            tenantIdList.add(LoginContextHolder.me().getSysLoginUser().getTenantId());
            tenantIdList.add(1l);
            queryWrapper.in(Banner::getTenantId,tenantIdList);
        }
        queryWrapper.orderByAsc(Banner::getTenantId);
        queryWrapper.orderByAsc(Banner::getSort);



        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<Banner> list(BannerParam bannerParam) {

        // 构造条件
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Banner::getDelFlag, PublicCommon.删除);
        if(!LoginContextHolder.me().isSuperAdmin()){
            List<Long> tenantIdList = new ArrayList<>();
            tenantIdList.add(LoginContextHolder.me().getSysLoginUser().getTenantId());
            tenantIdList.add(1l);
            queryWrapper.in(Banner::getTenantId,tenantIdList);
        }
        queryWrapper.orderByAsc(Banner::getTenantId);
        queryWrapper.orderByAsc(Banner::getSort);

        return this.list(queryWrapper);
    }

    @Override
    public void add(BannerParam bannerParam) {

        // 将dto转为实体
        Banner banner = new Banner();
        BeanUtil.copyProperties(bannerParam, banner);
        banner.setTenantId(LoginContextHolder.me().getSysLoginUser().getTenantId());
        this.save(banner);
    }

    @Override
    public void delete(BannerParam bannerParam) {

        if(!LoginContextHolder.me().isSuperAdmin()){
            Banner banner = this.getById(bannerParam.getId());
            if (banner.getTenantId()==1){
                throw new CustomException(CoreExceptionEnum.BANNER_ERROR);
            }

        }
        // 根据id查询实体
        Banner banner = this.queryBanner(bannerParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(bannerParam, banner);

        this.updateById(banner);
    }

    @Override
    public void edit(BannerParam bannerParam) {
        if(!LoginContextHolder.me().isSuperAdmin()){
            Banner banner = this.getById(bannerParam.getId());
            if (banner.getTenantId()==1){
                throw new CustomException(CoreExceptionEnum.BANNER_ERROR);
            }

        }

        // 根据id查询实体
        Banner banner = this.queryBanner(bannerParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(bannerParam, banner);

        this.updateById(banner);
    }

    @Override
    public Banner detail(BannerParam bannerParam) {
        return this.queryBanner(bannerParam);
    }


    /**
     * 获取轮播图
     *
     * @author 刘耀
     * @since 2022/02/01 11:05
     */
    private Banner queryBanner(BannerParam bannerParam) {
        Banner banner = this.getById(bannerParam.getId());
        if (ObjectUtil.isEmpty(banner)) {
            throw new ServiceException(BannerExceptionEnum.NOT_EXISTED);
        }
        return banner;
    }
}